#import "OmPersonEditAlertView.h"
@interface OmPersonEditAlertView ()
{
    UIView *_contentView;
    UIView *_dismissView;
    NSInteger _currentIndex;
    NSArray *_titleArr;
    NSInteger _viewType;
}
@end
@implementation OmPersonEditAlertView
- (instancetype)init {
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        _currentIndex = 0;
        [self _layoutMainView];
    }
    return self;
}
+ (OmPersonEditAlertView *)showInViewWithType:(NSInteger)type selectBlock:(LoveAndEducationBlock)selectBlock {
    OmPersonEditAlertView *alert = [[OmPersonEditAlertView alloc]init];
    alert.selectBlock = selectBlock;
    [alert customContentViewType:type];
    [[UIApplication sharedApplication].keyWindow addSubview:alert];
    [alert showInView];
    return alert;
}
#pragma mark - 业务处理
- (void)customContentViewType:(NSInteger)type {
    _viewType = type;
    CGFloat width = 0;
    if (type == 0) {
        width = 160;
        _titleArr = @[ASLocalizedString(@"单身"),ASLocalizedString(@"已婚"),ASLocalizedString(@"离婚"),ASLocalizedString(@"保密")];
    }else {
        width = 250;
        _titleArr = @[ASLocalizedString(@"高中"),ASLocalizedString(@"大学毕业"),ASLocalizedString(@"学士学位"),ASLocalizedString(@"硕士学位以上")];
    }
    [self customContentData:width];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.mas_equalTo (CGSizeMake(width, 221));
    }];
}
- (void)customContentData:(CGFloat)width {
    for (NSInteger i = 0; i < _titleArr.count; i++) {
        UIButton *btn = [self customSelectBtn:i title:[_titleArr objectAtIndex:i]];
        btn.frame = CGRectMake(0, 55.5 * i, width, 54.5);
    }
}
- (void)selectClick:(UIButton *)btn {
    btn.selected = YES;
    _currentIndex = btn.tag - 300;
    [self performSelector:@selector(sureHandler) withObject:nil afterDelay:0.15];
}
- (UIButton *)customSelectBtn:(NSInteger)flag title:(NSString *)title {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom].fp_addTargetActionForControlEvents(self, @selector(selectClick:), UIControlEventTouchUpInside);
    [btn setBackgroundColor:[UIColor whiteColor]];
    btn.tag = 300 + flag;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [btn setImage:IMAGE_NAMED(@"OmDeselect") forState:UIControlStateNormal];
    [btn setImage:IMAGE_NAMED(@"OmSelect") forState:UIControlStateSelected];
    CGFloat leftImg = 26;
    CGFloat leftTitle = 40;
    if (_viewType == 0) {
        leftImg = 30;
        leftTitle = 50;
    }
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, leftTitle, 0, 0)];
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0, leftImg, 0, 0)];
    [_contentView addSubview:btn];
    return btn;
}
- (void)cancelHandler {
    [self hiddenInView];
}
- (void)sureHandler {
    if (self.selectBlock) {
        NSString *title = [_titleArr objectAtIndex:_currentIndex];
        if (_viewType == 0) {
            title = [title stringByReplacingOccurrencesOfString:@" " withString:@""];
        }
        self.selectBlock(title);
    }
    [self hiddenInView];
}
- (void)showInView {
    self.hidden = NO;
    _contentView.transform = CGAffineTransformMakeScale(0.5, 0.5);
    [UIView animateWithDuration:0.05
                     animations:^{
                         self->_dismissView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
                         self->_contentView.transform = CGAffineTransformMakeScale(1.0, 1.0);
                     }];
}
- (void)hiddenInView {
    [UIView animateWithDuration:0.2 animations:^{
        self->_dismissView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        self->_contentView.transform = CGAffineTransformMakeScale(0.5, 0.5);
    }completion:^(BOOL finished) {
        self.hidden = YES;
        [self removeFromSuperview];
    }];
}
- (void)_layoutMainView {
    self.backgroundColor = [UIColor clearColor];
    _dismissView = [[UIView alloc]init];
    [self addSubview:_dismissView];
    _dismissView.frame = self.bounds;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelHandler)];
    [_dismissView addGestureRecognizer:tap];
    _contentView = [[UIView alloc]init];
    _contentView.backgroundColor = HexadecimalColor(@"#E6E6E6");
    ViewRadius(_contentView, 6);
    _contentView.layer.masksToBounds = YES;
    [self addSubview:_contentView];
}
@end
